package com.ruoyi.service;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.entity.OrderClearingItem;
import com.ruoyi.entity.SxfOrderClearingItem;
import com.ruoyi.mapper.OrderClearingItemMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.param.ClearingParam;
import com.sxftech.utils.CopyBeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.bean.OrderClearingItemFrom;
import com.sxftech.resp.ResponseData;
/**
 * <p>
 * 订单清分汇总项目表 服务实现类
 * </p>
 *
 * @author liuxing
 * @since 2020-10-30
 */
@Service
@DataSource(value = DataSourceType.ORDER)
public class OrderClearingItemService extends ServiceImpl<OrderClearingItemMapper, OrderClearingItem> {

	@Autowired
	private OrderClearingItemMapper orderClearingItemMapper;
	@Autowired
	private SxfOrderClearingItemService sxfOrderClearingItemService;

	public ResponseData create(OrderClearingItemFrom orderClearingItem) {
		OrderClearingItem entity = transformationFrom(orderClearingItem);
		this.save(entity);
		return ResponseData.success("新增成功");
	}
	
	public ResponseData modify(OrderClearingItemFrom orderClearingItem) {
		if(findById(orderClearingItem.getId())== null) {
			return ResponseData.fail("没有找到对应记录");
		}
		OrderClearingItem entity = transformationFrom(orderClearingItem);
		entity.setId(orderClearingItem.getId());
		this.updateById(entity);
		return ResponseData.success("修改成功");
	}

	public OrderClearingItem transformationFrom(OrderClearingItemFrom orderClearingItem) {
		OrderClearingItem entity = new OrderClearingItem();
		entity.setMerchantCode(orderClearingItem.getMerchantCode());
		entity.setClearingType(orderClearingItem.getClearingType());
		entity.setClearingTime(orderClearingItem.getClearingTime());
		entity.setAmount(orderClearingItem.getAmount());
		entity.setAlipayAccount(orderClearingItem.getAlipayAccount());
		entity.setRemark(orderClearingItem.getRemark());
		entity.setType(orderClearingItem.getType());
		entity.setCreateTime(orderClearingItem.getCreateTime());
		entity.setModifyTime(orderClearingItem.getModifyTime());
		return entity;
	}

	public OrderClearingItemFrom transformationBean(OrderClearingItem orderClearingItem) {
		OrderClearingItemFrom from = new OrderClearingItemFrom();
		from.setMerchantCode(orderClearingItem.getMerchantCode());
		from.setClearingType(orderClearingItem.getClearingType());
		from.setClearingTime(orderClearingItem.getClearingTime());
		from.setAmount(orderClearingItem.getAmount());
		from.setAlipayAccount(orderClearingItem.getAlipayAccount());
		from.setRemark(orderClearingItem.getRemark());
		from.setType(orderClearingItem.getType());
		from.setCreateTime(orderClearingItem.getCreateTime());
		from.setModifyTime(orderClearingItem.getModifyTime());

		return from;
	}

	
	public OrderClearingItemFrom findById(String id) {
		OrderClearingItem entity = this.getOne(new QueryWrapper<OrderClearingItem>().lambda()
				.eq(OrderClearingItem::getId, id)
				);
		if(entity == null) return null;
		return transformationBean(entity);
	}
	
	public ResponseData remove(String id) {
		if(findById(id)== null) {
			return ResponseData.fail("没有找到对应记录");
		}
		OrderClearingItem orderClearingItem = new OrderClearingItem();
		orderClearingItem.setId(id);
		this.updateById(orderClearingItem);
		return ResponseData.success("删除成功");
	}
	
	public IPage pageList(Integer page, Integer size, OrderClearingItemFrom ibean) {
		return this.page(new Page(page, size), new QueryWrapper<OrderClearingItem>().lambda()
				.eq(StringUtils.isNotEmpty(ibean.getMerchantCode()), OrderClearingItem::getMerchantCode, ibean.getMerchantCode())
				.eq(ibean.getClearingType() != null, OrderClearingItem::getClearingType, ibean.getClearingType())
				.eq(StringUtils.isNotEmpty(ibean.getClearingTime()), OrderClearingItem::getClearingTime, ibean.getClearingTime())
				.eq(ibean.getAmount() != null, OrderClearingItem::getAmount, ibean.getAmount())
				.eq(StringUtils.isNotEmpty(ibean.getAlipayAccount()), OrderClearingItem::getAlipayAccount, ibean.getAlipayAccount())
				.eq(StringUtils.isNotEmpty(ibean.getRemark()), OrderClearingItem::getRemark, ibean.getRemark())
				.eq(ibean.getType() != null, OrderClearingItem::getType, ibean.getType())
				.eq(ibean.getCreateTime() != null, OrderClearingItem::getCreateTime, ibean.getCreateTime())
				.eq(ibean.getModifyTime() != null, OrderClearingItem::getModifyTime, ibean.getModifyTime())
				);
	}


	public Page<OrderClearingItem> selectList(ClearingParam clearingParam) {
		Page<OrderClearingItem> page = new Page<>(clearingParam.getPage(),clearingParam.getSize());
		Page<OrderClearingItem> page1 = orderClearingItemMapper.page(page,clearingParam.getMerchantCode(),clearingParam.getClearingTime(),clearingParam.getClearingType());
		return page1;
	}

	public OrderClearingItem updateClear(String id){

		OrderClearingItem orderClearingItem = getById(id);
		if(orderClearingItem!=null){
			if(orderClearingItem.getClearingType().equals(1)&&orderClearingItem.getType().equals(0)){
				orderClearingItem.setClearingType(0);
				orderClearingItem.setType(1);
				updateById(orderClearingItem);
				SxfOrderClearingItem sxfOrderClearingItem = new SxfOrderClearingItem();
				CopyBeanUtils.copyNonNullPropertites(orderClearingItem,sxfOrderClearingItem);
				sxfOrderClearingItem.setClearId(sxfOrderClearingItem.getId());
				sxfOrderClearingItemService.save(sxfOrderClearingItem);
			}else if(orderClearingItem.getClearingType().equals(0)&&orderClearingItem.getType().equals(1)){
				orderClearingItem.setClearingType(1);
				orderClearingItem.setType(0);
				updateById(orderClearingItem);
				SxfOrderClearingItem sxfOrderClearingItem = new SxfOrderClearingItem();
				CopyBeanUtils.copyNonNullPropertites(orderClearingItem,sxfOrderClearingItem);
				sxfOrderClearingItem.setClearId(sxfOrderClearingItem.getId());
				sxfOrderClearingItemService.save(sxfOrderClearingItem);
			}
		}
		return orderClearingItem;
	}
}
